package com.cskaoyan.controller;


import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;
import com.cskaoyan.bean.baseresultVO.BaseRespVo;
import com.cskaoyan.bean.domain.Order;
import com.cskaoyan.bean.order.Chanal.Chanal;
import com.cskaoyan.bean.order.OrderId.OrderId;
import com.cskaoyan.bean.order.OrderRefund.OrderRefund;
import com.cskaoyan.bean.order.baseorders.BaseOrder.BaseOrder;
import com.cskaoyan.bean.order.ordership.OrderShioBo;
import com.cskaoyan.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author looper
 * @since 2021-11-29
 */
@RestController
@RequestMapping("admin/order")
public class OrderController {


    @Autowired
    OrderService service;


    @GetMapping("list")
    public BaseRespVo gettest(BaseParam baseParam, Order order,
                              String[] timeArray, String start, String end,
                              String[] orderStatusArray) {
        //timeArray不知道是什么是时间 == end start
        //orderStatusArray是订单状态
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;
        try {
            startTime = format.parse(start);
            endTime = format.parse(end);
        } catch (ParseException e) {
            System.out.println("时间转换错了");
        }
        BaseData baseData = service.getOrderList(baseParam, order, orderStatusArray, startTime, endTime);
        return BaseRespVo.ok(baseData);
    }

    @GetMapping("channel")
    public BaseRespVo getChanal() {
        //返回所有快递
        List<Chanal> list = new ArrayList<>();
        list.add(new Chanal("ZTO", "中通快递"));
        list.add(new Chanal("YTO", "圆通速递"));
        list.add(new Chanal("YD", "韵达速递"));
        list.add(new Chanal("YZPY", "邮政快递包裹"));
        list.add(new Chanal("EMS", "EMS"));
        list.add(new Chanal("ZJS", "宅急送"));
        list.add(new Chanal("TNT", "TNT快递"));
        list.add(new Chanal("UPS", "UPS"));
        list.add(new Chanal("DHL", "DHL"));
        list.add(new Chanal("FEDEX", "FEDEX联邦(国内件)"));
        list.add(new Chanal("FEDEX_GJ", "FEDEX联邦(国际件)"));
        return BaseRespVo.ok(list);
    }

    @GetMapping("detail")
    public BaseRespVo getDetail(Integer id) {
        //这个是order的id,查出对应的订单 货物 以及 用户 (一个类里,有一个order对象,一个ordergoods集合对象,user对象)
        BaseOrder baseOrder = service.getOrderDetail(id);
        return BaseRespVo.ok(baseOrder);
    }

    @PostMapping("delete")
    public BaseRespVo getDelete(@RequestBody OrderId orderId) {
        //逻辑删除,并且修改updatetime
        Integer id = orderId.getOrderId();
        service.deleteByUpdate(id);
        return BaseRespVo.ok(null);

    }

    @PostMapping("refund")
    public BaseRespVo getRefund(@RequestBody OrderRefund orderRefund) {
        //老师的页面上看不出逻辑,只有退款失败这一个样式
        Integer orderId = orderRefund.getOrderId();
        Integer refundMoney = orderRefund.getRefundMoney();
        return BaseRespVo.buok(621);
    }

    @PostMapping("ship")
    public BaseRespVo getShip(@RequestBody OrderShioBo orderShioBo) {
        //根据orderid 也就是order的id 修改快递信息:快递名字√  快递单号√ 订单状态301 增加修改时间

        Integer code = service.getShip(orderShioBo);
        return BaseRespVo.ok(null);
    }


}

